/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/11/12
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    form.render(null, "zmn-permit-form");

    // 自定义验证规则
    form.verify({
        title: function (value) {
            if (value.length < 2) {
                return "分类名称不能少于2个字符";
            }
            ;
        },
        sort: function (value) {
            if (!/^[0-9]+$/.test(value)) {
                return "排序值只能是正整数";
            }
        },
        parentId: function (value) {
            if (!/^[1-9]\d*$/.test(value)) {
                return "上级菜单ID只能是大于1的正整数";
            }
        }
    });

    // 提交
    form.on('submit(zmn-permit-form-submit)', function (obj) {
        var data = obj.field;
        var url = "/mc/permit/addEdit.action";
        var permitId = $("#permitId").val();

        if (permitId == "" || permitId == 0) {
            // "1000000/1000001/1000002/1000004,1000017,1000010"
            if (data.moduleId == null || data.moduleId == "") {
                zmnUtil.alert("请选择模块");
                return false;
            }
            if (data.moduleId.split(",").length > 1) {
                zmnUtil.alert("模块只能选择一个");
                return false;
            }

            var id = data.moduleId.split("/");
            data.moduleId = id[id.length - 1];
        }
        var isButton=$('#isButton input[name="button"]:checked ').val();
        if (zmnUtil.isEmptyNull(isButton)) {
            zmnUtil.alert("请选择权限是菜单或者按钮");
            return false;
        }

        var openId = layer.open({type: 3});

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success: function (json) {
                layer.close(openId);
                layer.msg(json.message);
                if (json.status == 200 && json.id > 0) {
                    zmnPage.direct("/mc/permit/detail.action?permitId=" + json.id, 1500);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        })
        return false;
    });

    //对外暴露的接口
    exports('art_permit_detail', {});
});